/*
 * DeliveryListItem.java
 * business
 *
 * Created by ChenTao on 2017/2/28.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Order;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.response.DeliveryResponse;
import com.yidiandao.business.manager.AgentsManager;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.DeliveryHolder;
import com.yidiandao.business.ui.widget.view.OrderGoodsInfoLayout;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import java.util.List;

/**
 * 我是代理商 配送订单
 */
public class DeliveryListItem
        extends DisplayListItem<DeliveryHolder, DeliveryResponse.Data.OrderListBean> {

    public DeliveryListItem(DeliveryResponse.Data.OrderListBean s) {
        super(s);
    }

    @Override
    public void onShow(final Context context, DeliveryHolder holder) {
        Shop supplierInfo = data.getSupplierInfo();

        final Activity activity = (Activity) context;
        holder.mTvAgentLevel.setVisibility(View.VISIBLE);
        //商店图
        ImageLoader.loadShopLogo(context, holder.mIvAgentLogo, supplierInfo.getShopLogo());
        //商店名
        holder.mTvAgentName.setText(supplierInfo.getSupplierName());
        //商品信息
        final List<Goods> goodsList = data.getGoodsList();


        holder.mInfoLayout.setData(goodsList);

        final Order orderInfo = data.getOrderInfo();
        //订单号
        holder.mTvAgentLevel.setText(orderInfo.getOrderSn());

        //支付方式
        holder.mTvDeliveryPayway.setText(orderInfo.getPayName());
        //商家
        holder.mTvDeliveryShop.setText(orderInfo.getDpname());
        //地址
        holder.mTvDeliveryAddress.setText(orderInfo.getAddress());
        //收货人
        holder.mTvDeliveryConsinee.setText(orderInfo.getConsignee());
        //电话
        holder.mTvDeliveryPhone.setText(orderInfo.getTel());
        //查看配送单的链接
        final String url = orderInfo.getUrl();

        //判断发货按钮是否可见  0不可见
        holder.mTvDispatch.setVisibility(
                orderInfo.getIsSendOrder() != 0 ? View.VISIBLE : View.GONE);
        //判断收款按钮是否可见  0不可见
        holder.mTvReceipt.setVisibility(orderInfo.getIsPayOrder() != 0 ? View.VISIBLE : View.GONE);

        if (null != goodsList && goodsList.size() != 0) {
            String notice = goodsList.get(0).getNotice();
            if (!TextUtils.isEmpty(notice) && notice.equals(
                    UIUtils.getString(R.string.order_canceled))) {
                holder.mTvDispatch.setVisibility(View.GONE);
            }
        }
        /*  //判断查看配送单按钮存在
        holder.mTvDelivery.setVisibility(!TextUtils.isEmpty(url) ? View.VISIBLE : View.GONE);*/

        //--------------处理点击事件-----------------
        //商品详情
        holder.mInfoLayout.setOnItemClickListener(
                new OrderGoodsInfoLayout.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position, String goodsId) {
                        GoodsDetailActivity.actionStart(activity, goodsId);
                    }
                });

        //查看配送单
        holder.mTvDelivery.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UmengUtils.onEvent(context, R.string.umeng_agents_delivery);
                        UrlActivity.actionStartFreeScreenOrientation((Activity) context, url);
                    }
                });

        //收款
        holder.mTvReceipt.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UmengUtils.onEvent(context, R.string.umeng_agents_get_money);
                        AgentsManager.getInstance()
                                .onButtonClick(
                                        AgentsManager.TYPE.RECEIVE_MONEY, orderInfo.getOrderId());
                    }
                });

        //发货
        holder.mTvDispatch.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UmengUtils.onEvent(context, R.string.umeng_agents_send_goods);
                        AgentsManager.getInstance()
                                .onButtonClick(
                                        AgentsManager.TYPE.DISPATCH_GOODS, orderInfo.getOrderId());
                    }
                });
    }

    @Override
    public String getDisplayItemTag() {
        return DeliveryListItem.class.getSimpleName();
    }

    @Override
    public Class<DeliveryHolder> getViewHolderClazz() {
        return DeliveryHolder.class;
    }
}
